package org.gas.barapraja.handler;

import org.apache.log4j.Logger;
import org.gas.barapraja.proc.PduProcessor;

import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.Response;
import com.logica.smpp.pdu.WrongLengthOfStringException;

public class BindRequestHandler extends RequestHandlerAdapter{

	private static final Logger logger=Logger.getLogger(BindRequestHandler.class);
	
	
	public BindRequestHandler(PduProcessor processor) {
		super();
		setPduProcessor(processor);
	}

	@Override
	public int handleRequest(BindRequest request) {
		logger.info("accepting bind request ");
		int mode=request.getCommandId();
		int status=0;
		if(mode==Data.BIND_RECEIVER||mode==Data.BIND_TRANSCEIVER||mode==Data.BIND_TRANSMITTER){
			BindResponse response=(BindResponse) request.getResponse();
			status=validator.validate(request);
			if(status==Data.ESME_ROK){
				pduProcessor.serverResponse(response);
			}else{
				try {
					response.setSystemId("Gas server");
				} catch (WrongLengthOfStringException e) {
					
					e.printStackTrace();
				}
				pduProcessor.serverResponse(response);
				pduProcessor.stop();
			}
		}else{
			if(request.canResponse()){
				status=Data.ESME_RINVBNDSTS;
				Response response=request.getResponse();
				pduProcessor.serverResponse(response);
			}else{
				
			}
			pduProcessor.stop();
			
		}
		return status;
	}
	
	
}
